#include "bsp_g3507std.h"
#include "ti_msp_dl_config.h"
#include "user_main.h"
#include "./../order_sensor/order_sensor_bsp.h"


uint32_t g_g3507_board_order_array[G3507_BOARD_ORDER_ARRAY_LEN];
uint32_t g_g3507_board_sensor_array[G3507_BOARD_SENSOR_ARRAY_LEN];

////////////////////////////////////
////////////////////////////////////
//rpm
volatile uint32_t g_pulse_period_buf[ENGINE_RPM_BUF_LEN] = {0};
volatile uint32_t g_current_count_10us = 0;

void GROUP1_IRQHandler(void)
{
    switch (DL_Interrupt_getPendingGroup(DL_INTERRUPT_GROUP_1)) {
        case GPIO_FREQ_freq_0_IIDX:
            if ((DL_GPIO_readPins(GPIO_FREQ_PORT, GPIO_FREQ_freq_0_PIN) & GPIO_FREQ_freq_0_PIN) == 0) 
            {
                static uint32_t _last_count_10us = 0;

                for (uint8_t i = 1; i < ENGINE_RPM_BUF_LEN; i++)
                {
                    g_pulse_period_buf[i - 1] = g_pulse_period_buf[i];
                }

                if (g_current_count_10us >= _last_count_10us)
                {
                    g_pulse_period_buf[ENGINE_RPM_BUF_LEN - 1] = (g_current_count_10us - _last_count_10us);
                }
                else if (g_current_count_10us < _last_count_10us)
                {
                    g_pulse_period_buf[ENGINE_RPM_BUF_LEN - 1] = ((0xffffffff - _last_count_10us + 1) + g_current_count_10us);
                }

                _last_count_10us = g_current_count_10us;

                if ( 100000 / g_pulse_period_buf[ENGINE_RPM_BUF_LEN - 1] < MIN_FREQ ) { //计算转速
                    for (uint8_t i=0; i< ENGINE_RPM_BUF_LEN; i++) {
                        g_pulse_period_buf[i] = 0;
                    }
                }

                bsp_timeout_ms( IDLE );
            }
            break;
    }
}

uint32_t bsp_timeout_ms( uint8_t flag )
{
    static uint8_t _current_state = IDLE;
    static uint32_t _start_ticks = 0;

    if( _current_state == IDLE && flag == IDLE )
    {
        return 0;
    }
    else if( _current_state == IDLE && flag == RUN )
    {
        _current_state = RUN;
        _start_ticks = g_ticks_inc;
        return 0;
    }
    else if( _current_state == RUN && flag == IDLE )
    {
        _current_state = IDLE;
        return (g_ticks_inc - _start_ticks);
    }
    else if( _current_state == RUN && flag == RUN )
    {
        return (g_ticks_inc - _start_ticks);
    }
    return 0;
}

uint16_t bsp_freq_cal(void)
{
	uint8_t i;
    uint32_t sum = 0 ;
    uint32_t pulse_period_final = 0;
    uint16_t generator_freq = 0;

    // 判断当前转速是否低于计算下限
    if ( g_pulse_period_buf[ ENGINE_RPM_BUF_LEN - 1 ]
      && bsp_timeout_ms( RUN ) > PULSE_PERIOD_MS_MIN)
    { 
        for (i=0; i< ENGINE_RPM_BUF_LEN; i++) {
            g_pulse_period_buf[i] = 0;
        }
        pulse_period_final = 0;
        return 0;
    }
    
    for (int i = 0; i < ENGINE_RPM_BUF_LEN ; i++) {
        sum += g_pulse_period_buf[i];
    }

    pulse_period_final = sum / (ENGINE_RPM_BUF_LEN);


    if (pulse_period_final > 0) {
        return 100000 / pulse_period_final;   // 10us 的倒数 为 100000
    }
    else {
        return 0;
    }
}


int g3507_board_get_sensor( uint32_t *board_sensor_array , uint32_t board_sensor_array_len  )
{
    board_sensor_array[0] = bsp_freq_cal();
    return 0;
}

int g3507_board_set_order( uint32_t *board_order_array  , uint32_t board_order_array_len  )
{

    return 0;
}
